<?php


namespace app\api\controller;

use app\api\model\StoreModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Request;
use app\api\validate\StoreValidate;

class Store extends Common
{
    /**
     * @param Request $request
     * @param StoreModel $storeModel
     * @return \think\response\Json
     */
    public function store(Request $request, StoreModel $storeModel)
    {
        $validate = new StoreValidate();
        if (!$validate->check($request->param())) {
            return json(['code' => 500, 'msg' => $validate->getError()]);
        }
        $res = $storeModel::create($request->param());
        if (!empty($res)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**
     * @param StoreModel $storeModel
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function index(Request $request, StoreModel $storeModel)
    {
        $data = $request->param();
        return $this->returnJson(200, 'ok', $storeModel->storeList($data));
    }

    /**
     * @param $id
     * @param Request $request
     * @param StoreModel $storeModel
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function update($id, Request $request, StoreModel $storeModel)
    {
        $data = $request->param();
        $validate = new StoreValidate();
        if (!$validate->check($data)) {
            return json(['code' => 500, 'msg' => $validate->getError()]);
        }
        $res = $storeModel::where('id', $id)->update($data);
        if (!empty($res)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    public function getStoreInfo($id, Request $request, StoreModel $storeModel)
    {
        try {
            $res = $storeModel->findOrFail($id);
            return $this->returnJson(200, 'ok', $res);
        } catch (DataNotFoundException $e) {
            return json(['code' => 500, 'msg' => '失败']);
        } catch (ModelNotFoundException $e) {
            return json(['code' => 500, 'msg' => '失败']);
        } catch (DbException $e) {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**
     * @param Request $request
     * @param StoreModel $storeModel
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function delete($id, Request $request, StoreModel $storeModel)
    {
        $store = $storeModel->findOrFail($id);
        $res = $store->delete();
        if (!empty($res)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    public function getAllStore(StoreModel $storeModel)
    {
        $list = $storeModel->getAllStore();
        return $this->returnJson(200, '成功', $list);
    }

    public function getAllKeeper(StoreModel $storeModel)
    {
        $list = $storeModel->getAllKeeper();
        return $this->returnJson(200, '成功', $list);
    }
}
